package com.cms.interceptor;

import com.cms.plugin.hook.HookServices;
import com.cms.common.hook.vo.HookParams;
import com.cms.common.hook.vo.HookResult;
import com.cms.html.HtmlRenderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.thymeleaf.context.Context;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Copyright (c) Since 2024 ThinkCms - All Rights Reserved
 *
 * @author what
 * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
 * @Date 2024/10/31 11:09
 */

@Component
@Order(1)
public class HookHtmlInterceptor implements HandlerInterceptor {

    String sessionKey = "web_login_token";

    @Autowired
    HtmlRenderService htmlRenderService;


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //注册进入action之前的hook
        String hook_key = request.getRequestURI().replace("/", " ").trim().replace(" ", ".") + ".request";
        System.out.println("hook_key:" + hook_key);
        HookParams hookParams = new HookParams();
        hookParams.getRequestParams().put("request", request);
        HookResult hookResult = HookServices.regHook(hook_key, hookParams);
        if (hookResult.getCode() != 0) {
            throw new Exception(hookResult.getMsg());
        }
        return true; // 如果返回false，则停止流程，不继续执行
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

        if (modelAndView != null) {
            if (modelAndView.getViewName().startsWith("redirect:")) {//重定向处理
                return;
            }
        }
        //注册执行完成后，在输出数据之前的hook
        String uri = request.getRequestURI();
        String hook_key = uri.replace("/", " ").trim().replace(" ", ".") + ".response";

        System.out.println("hook_key:" + hook_key);
        HookParams hookParams = new HookParams();
        hookParams.getRequestParams().put("request", request);
        hookParams.getRequestParams().put("response", response);
        hookParams.getRequestParams().put("modelAndView", modelAndView);
        HookResult hookResult = HookServices.regHook(hook_key, hookParams);
        if (hookResult.getCode() != 0) {
            throw new Exception(hookResult.getMsg());
        }

        if (modelAndView != null) {
            if (!request.getRequestURI().equals("/admin/logout")) {
                modelAndView.getModel().put("menu_active", request.getRequestURI());
            }
            Context context = new Context();
            modelAndView.getModel().put("template_context", context);
            //处理页面hook替换成对应的页面渲染内容
            String html = htmlRenderService.renderHtml(modelAndView.getViewName(), modelAndView.getModel(), context);
            //注册执行动态页面hook
            HookParams hookParamHtmls = new HookParams();
            hookParamHtmls.getRequestParams().put("request", request);
            hookParamHtmls.getRequestParams().put("response", response);
            hookParamHtmls.getRequestParams().put("modelAndView", modelAndView);
            hookParamHtmls.getRequestParams().put("html", html);
            System.out.println("hook_key:" + hook_key + ".html");
            HookResult hookResult2 = HookServices.regHook(hook_key + ".html", hookParamHtmls);
            if (hookResult2.getCode() != 0) {
                throw new Exception(hookResult2.getMsg());
            }
            html = (String) hookParamHtmls.getRequestParams().get("html");
            modelAndView.addObject("htmlContent", html);
        }


    }

}
